import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginPage from '../views/LoginPage.vue'
import MainLayout from '../components/MainLayout.vue'
import CountryList from "@/views/CountryList.vue";
import HomePage from "@/views/HomePage.vue";
import TemplatePage from "@/views/TemplatePage.vue";
import AddTemplate from "@/views/AddTemplate.vue";
import FieldPage from "@/views/FieldPage.vue";
import EditTemplate from "@/views/EditTemplate.vue";
import VisaProduct from "@/views/VisaProduct.vue";
import AddVisaProduct from "@/views/AddVisaProduct.vue";
import EditVisaProduct from "@/views/EditVisaProduct.vue";
import FieldLibrary from "@/views/FieldLibrary.vue";
import OtherProduct from "@/views/OtherProduct.vue";
import Order from '../views/Order.vue'
import Customer from '@/views/Customer.vue' 
import Account from '@/views/Account.vue';  
import VisaMaterialRequirement from '@/views/VisaMaterialRequirement.vue'
import MaterialTemplateManagement from '@/views/MaterialTemplateManagement.vue'
import VisitStats from '@/views/statistics/VisitStats.vue'
import ProductStats from '@/views/statistics/ProductStats.vue'
import OrderStats from '@/views/statistics/OrderStats.vue'
import CustomerStats from '@/views/statistics/CustomerStats.vue'
import CarouselManage from '@/views/platform/CarouselManage.vue'
import MessageTemplate from '@/views/platform/MessageTemplate.vue'
import PlatformSettings from '@/views/platform/PlatformSettings.vue'


Vue.use(VueRouter)


const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: LoginPage,
    meta: { requiresAuth: false }
  },
  {
    path: '/main',
    name: 'main',
    component: MainLayout,
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'home',
        component: HomePage,
        meta: { keepAlive: true, requiresAuth: true },
      },
      {
        path: 'country',
        name: 'country',
        component: CountryList,
        meta: { title: '国家列表', requiresAuth: true },
      },
      {
        path: 'template',
        name: 'template',
        component: TemplatePage,
        meta: { title: '申请表模板', requiresAuth: true },
      },
      {
        path: 'template/fieldLibrary',
        name: 'fieldLibrary',
        component: FieldLibrary,
        meta: {title:"字段库", requiresAuth: true},
      },
      {
        path: 'template/addTemplate',
        name: 'addTemplate',
        component: AddTemplate,
        meta: { title: '添加申请表模板', requiresAuth: true },
      },
      {
        path: 'template/editTemplate/:id/field',
        name: 'editTemplateFieldLibrary',
        component: FieldPage,
        meta: { title: '字段表', requiresAuth: true }
      },
      {
        path: 'template/editTemplate/:id',
        name: 'editTemplate',
        component: EditTemplate,
        meta: { title: '编辑申请表模板', requiresAuth: true },
      },
      {
        path: 'visaProduct',
        name: 'visaProduct',
        component: VisaProduct,
        meta: { title: '签证产品', requiresAuth: true },
      },
      {
        path: 'visaProduct/addVisaProduct',
        name: 'addVisaProduct',
        component: AddVisaProduct,
        meta: { title: '添加签证产品', requiresAuth: true },
      },
      {
        path: 'visaProduct/editVisaProduct/:id',
        name: 'editVisaProduct',
        component: EditVisaProduct,
        meta: { title: '编辑签证产品', requiresAuth: true },
      },
      {
        path: 'otherProduct',
        name: 'otherProduct',
        component: OtherProduct,
        meta: {title: '其他产品', requiresAuth: true},
      },
      {
        path: 'visa-material-requirement',
        name: 'visaMaterialRequirement',
        component: VisaMaterialRequirement,
        meta: { title: '签证材料要求', requiresAuth: true },
      },
      {
        path: 'material-template',
        name: 'materialTemplate',
        component: MaterialTemplateManagement,
        meta: { title: '材料模板管理', requiresAuth: true },
      },
      {
        path: 'order',
        name: 'order',
        component: Order,
        meta: { requiresAuth: true }
      },
      {
        path: 'customer',
        name: 'customer',
        component: Customer,
        meta: {
          title: '客户管理',
          requiresAuth: true
        }
      },
      {
        path: 'visit-stats',
        name: 'visitStats',
        component: VisitStats,
        meta: { title: '访问统计', requiresAuth: true }
      },
      {
        path: 'product-stats',
        name: 'productStats',
        component: ProductStats,
        meta: { title: '产品统计', requiresAuth: true }
      },
      {
        path: 'order-stats',
        name: 'orderStats',
        component: OrderStats,
        meta: { title: '订单统计', requiresAuth: true }
      },
      {
        path: 'customer-stats',
        name: 'customerStats',
        component: CustomerStats,
        meta: { title: '客户统计', requiresAuth: true }
      },
      {
        path: 'carousel',
        name: 'carousel',
        component: CarouselManage,
        meta: { title: '轮播图管理', requiresAuth: true }
      },
      {
        path: 'message-template',
        name: 'messageTemplate',
        component: MessageTemplate,
        meta: { title: '消息模板', requiresAuth: true }
      },
      {
        path: 'platform-settings',
        name: 'platformSettings',
        component: PlatformSettings,
        meta: { title: '平台设置', requiresAuth: true }
      },
      {
        path: '/main/account',
        name: 'account',
        component: Account,
        meta: {
          title: '账号管理',
          requiresAuth: true
        }
      },
      {
        path: '/main/member',
        component: () => import('@/views/member/MemberList.vue'),
        name: 'member',
        meta: {
          title: '会员管理',
          requiresAuth: true
        }
      },
      {
        path: '/main/member/detail/:id',
        component: () => import('@/views/member/MemberDetail.vue'),
        name: 'MemberDetail',
        meta: { title: '会员详情', requiresAuth: true },
        hidden: true
      }
    ],
  },
  // 添加通配符路由处理404
  {
    path: '*',
    redirect: '/login'
  }
];

const router = new VueRouter({
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查是否有token
    const token = localStorage.getItem('token');
    if (!token) {
      // 没有token，跳转到登录页面
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 保存原本要访问的路径
      });
      return;
    }
  }
  
  // 如果访问登录页面但已经登录，跳转到主页
  if (to.path === '/login') {
    const token = localStorage.getItem('token');
    if (token) {
      next('/main');
      return;
    }
  }
  
  next();
});

export default router;
